﻿using ModelLibrary;
using ModelLibrary.Contrl;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

namespace IMSTools
{
    public partial class FmMain : Form
    {
        public FmMain()
        {
            InitializeComponent();
        }
        XTabControl xTabControl = new XTabControl();
        TabPage tabPage = new TabPage();
        Button buttonDj = new Button();
        Button buttonCx = new Button();
        private void FmMain_Load(object sender, EventArgs e)
        {
            XTabControl_Load();
        }
        private void XTabControl_Load()
        {
            //
            //xTabControl
            //
            this.xTabControl.Dock = DockStyle.Fill;
            //
            //tabPage
            //
            tabPage.Text = "工具菜单";
            //
            //buttonDj
            //
            buttonDj.Size = new Size(150, 150);
            buttonDj.Location = new Point(50, 50);
            buttonDj.Text = "单据";
            buttonDj.Font = new Font("楷体", 20);
            buttonDj.Click += ButtonDj_Click;
            //
            //buttonCx
            //
            buttonCx.Size = new Size(150, 150);
            buttonCx.Location = new Point(250, 50);
            buttonCx.Text = "查询";
            buttonCx.Font = new Font("楷体", 20);
            buttonCx.Click += ButtonCx_Click;

            xTabControl.Controls.Add(tabPage);
            this.plMain.Controls.Add(xTabControl);
            this.tabPage.Controls.Add(buttonDj);
            this.tabPage.Controls.Add(buttonCx);
        }

        private void ButtonCx_Click(object sender, EventArgs e)
        {
            string selectTabPageText = "查询1";
            string selectTabPageName = selectTabPageText + (xTabControl.TabPages.Count + 1).ToString();
            AddPages(selectTabPageText, selectTabPageName);
        }

        private void ButtonDj_Click(object sender, EventArgs e)
        {
            string selectTabPageText = "按钮1";
            string selectTabPageName = selectTabPageText + (xTabControl.TabPages.Count + 1).ToString();
            AddPages(selectTabPageText, selectTabPageName);
        }
        private void AddPages(string selectTabPageText, string selectTabPageName)
        {
            foreach (TabPage item in xTabControl.TabPages)
            {
                if (item.Text == selectTabPageText)
                {
                    xTabControl.SelectTab(item);
                    break;
                }
            }
            if (xTabControl.SelectedTab.Text != selectTabPageText)
            {
                TabPage sPage = GetPage(selectTabPageText, selectTabPageName);
                sPage.Name = selectTabPageName;
                xTabControl.Controls.Add(sPage);
                xTabControl.SelectTab(sPage);
            }
        }
        private TabPage GetPage(string PageText, string PageName)
        {
            TabPage newTabPage = new TabPage(PageText);
            FormInfo formInfo = new FormInfo();
            formInfo.Name = PageName;
            formInfo.Text = PageText;
            switch (PageText)
            {
                case "查询1":
                formInfo.FormType = FormTypeStyle.Query;
                    break;
                case "按钮1":
                    formInfo.FormType = FormTypeStyle.Receipt;
                    break;
            }

            
            DataTable dataTable = GetDataTable(formInfo);

            newTabPage.Controls.Add(GetForm(dataTable, formInfo));

            return newTabPage;
        }
        private DataTable GetDataTable(FormInfo formInfo)
        {
            DataTable dataTable = new DataTable();
            dataTable.Columns.Add(new DataColumn("CtrAlias", typeof(string)));
            dataTable.Columns.Add(new DataColumn("CtrID", typeof(string)));
            dataTable.Columns.Add(new DataColumn("CtrName", typeof(string)));
            dataTable.Columns.Add(new DataColumn("CtrType", typeof(string)));
            dataTable.Columns.Add(new DataColumn("Dock", typeof(string)));
            dataTable.Columns.Add(new DataColumn("Enable", typeof(string)));
            dataTable.Columns.Add(new DataColumn("FuncNo", typeof(string)));
            dataTable.Columns.Add(new DataColumn("ReadOnly", typeof(string)));
            dataTable.Columns.Add(new DataColumn("Text", typeof(string)));
            dataTable.Columns.Add(new DataColumn("TimeStack", typeof(string)));
            dataTable.Columns.Add(new DataColumn("Hight", typeof(int)));
            dataTable.Columns.Add(new DataColumn("Width", typeof(int)));
            dataTable.Columns.Add(new DataColumn("PointX", typeof(int)));
            dataTable.Columns.Add(new DataColumn("PointY", typeof(int)));

            dataTable.Rows.Add("Lable1", "1", "Name", "Label", "None", "1", formInfo.Name, "1", formInfo.Text, "test1", 25, 75, 20, 20);
            dataTable.Rows.Add("TextBox1", "2", "Name", "TextBox", "None", "1", formInfo.Name, "1", formInfo.Text, "test1", 25, 75, 100, 20);
            dataTable.Rows.Add("Button1", "3", "Name", "Button", "None", "1", formInfo.Name, "1", formInfo.Text, "test1", 25, 75, 180, 20);

            return dataTable;
        }
        private Form GetForm(DataTable dataTable, FormInfo formInfo)
        {
            Form form=new Form();
            switch (formInfo.FormType)
            {
                case FormTypeStyle.Receipt:
                    form = new FmReceipt();
                    ((FmReceipt)form).formQuit += FmMain_formQuit;
                    break;
                case FormTypeStyle.Query:
                    form = new FmQuery();
                    break;
            }
            form.TopLevel = false;
            form.Name = formInfo.Name;
            form.Text = formInfo.Text;
            form.Dock = DockStyle.Fill;
            form.FormBorderStyle = FormBorderStyle.None;

            ControlInfo controlInfo;
            foreach (DataRow dataRow in dataTable.Rows)
            {
                controlInfo = new ControlInfo();
                controlInfo.CtrAlias = dataRow["CtrAlias"].ToString();
                controlInfo.CtrID = dataRow["CtrID"].ToString();
                controlInfo.CtrName = dataRow["CtrName"].ToString();
                controlInfo.CtrType = dataRow["CtrType"].ToString();
                controlInfo.Dock = dataRow["Dock"].ToString();
                controlInfo.Enable = dataRow["Enable"].ToString();
                controlInfo.Event = GetEvents();
                controlInfo.FuncNo = dataRow["FuncNo"].ToString();
                controlInfo.ReadOnly = dataRow["ReadOnly"].ToString();
                controlInfo.Text = dataRow["Text"].ToString();
                controlInfo.TimeStack = dataRow["TimeStack"].ToString();
                controlInfo.Hight = (int)(dataRow["Hight"]);
                controlInfo.Width = (int)dataRow["Width"];
                controlInfo.PointX = (int)dataRow["PointX"];
                controlInfo.PointY = (int)dataRow["PointY"];

                switch (dataRow["CtrType"])
                {
                    case "Label":
                        form.Controls.Add(GetLabel(controlInfo));
                        break;
                    case "Button":
                        form.Controls.Add(GetButton(controlInfo));
                        break;
                    case "TextBox":
                        form.Controls.Add(GetTextBox(controlInfo));
                        break;
                    case "ComboBox":
                        break;
                    case "":
                        break;
                }
            }
            form.Show();
            return form;
        }

        /// <summary>
        /// tabPage内窗口点关闭的时候，同时移除tabPage
        /// </summary>
        /// <param name="form"></param>
        private void FmMain_formQuit(object form)
        {
            foreach (TabPage tabPage in xTabControl.TabPages)
            {
                if (tabPage.Text == form.ToString())
                {
                    this.xTabControl.Controls.Remove(tabPage);
                }
            }
        }
        private List<Events> GetEvents()
        {
            List<Events> listEvents = new List<Events>();


            return listEvents;
        }
        private Label GetLabel(ControlInfo controlInfo)
        {
            Label label = new Label();
            label.Name = controlInfo.CtrName;
            label.Text = controlInfo.Text;
            label.Location = new Point(controlInfo.PointX, controlInfo.PointY);
            label.Size = new Size(controlInfo.Width, controlInfo.Hight);

            return label;
        }
        private TextBox GetTextBox(ControlInfo controlInfo)
        {
            TextBox textBox = new TextBox();
            textBox.Name = controlInfo.CtrName;
            textBox.Text = controlInfo.Text;
            textBox.Location = new Point(controlInfo.PointX, controlInfo.PointY);
            textBox.Size = new Size(controlInfo.Width, controlInfo.Hight);

            return textBox;
        }
        private Button GetButton(ControlInfo controlInfo)
        {
            Button button = new Button();
            button.Name = controlInfo.CtrName;
            button.Text = controlInfo.Text;
            button.Location = new Point(controlInfo.PointX, controlInfo.PointY);
            button.Size = new Size(controlInfo.Width, controlInfo.Hight);

            return button;
        }
    }
}
